package com.casstime.ec.cloud.discount.restful;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.casstime.ec.cloud.discount.client.Userv2Client;
import com.casstime.ec.cloud.discount.dao.business.SupplierPromotionConfigRecordMapper;
import com.casstime.ec.cloud.discount.services.PromotionConfigService;
import com.casstime.ec.cloud.service.GenericService;
import com.casstime.ec.cloud.service.SupplierPromotionConfigService;
import com.casstime.ec.cloud.spi.PagedModels;
import com.casstime.ec.cloud.user.spi.models.User;
import com.casstime.ec.cloud.values.business.SupplierPromotionConfig;
import com.casstime.ec.cloud.values.business.SupplierPromotionConfigRecord;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * ClassName: DiscountServiceImpl 
 * Function: (金额促销实现类)
 * Reason: ADD REASON(可选).
 *  
 * @author wu.chen 
 * 2018年2月26日 上午11:02:13
 * @version   
 * @since JDK 1.8
 */
@RestController
public class SupplierPromotionConfigServiceRest extends GenericService implements SupplierPromotionConfigService {
	
	@Autowired
	private PromotionConfigService promotionConfigService;
	
	@Autowired
	private Userv2Client userv2Client;
	
	@Autowired
	private SupplierPromotionConfigRecordMapper supplierPromotionConfigRecordMapper;
	
	@Override
	public int insertSupplierPromotionConfig(@RequestBody SupplierPromotionConfig supplierPromotionConfig,
			@RequestParam("userLoginId") String userLoginId) throws Exception {
		User user = null;
		if (StringUtils.isNotBlank(userLoginId)) {
			user = userv2Client.getUser(userLoginId);
		} else {
			return 0;
		}
		return promotionConfigService.insertSupplierPromotionConfig(supplierPromotionConfig, user);
	}

	@Override
	public SupplierPromotionConfig getSupplierPromotionConfig(@RequestParam("productStoreId") String productStoreId,
			@RequestParam(value = "configType", required = false) String configType) throws Exception {
		
		return promotionConfigService.getSupplierPromotionConfig(productStoreId, configType);
	}

	@Override
	public PagedModels<SupplierPromotionConfigRecord> listSupplierPromotionConfigRecordById(@PathVariable("productStoreId") String productStoreId,
			   @RequestParam("pageSize") Integer pageSize,
			   @RequestParam("pageNum") Integer pageNum) throws Exception {
		Page<SupplierPromotionConfigRecord> pagedResult = PageHelper.startPage(pageNum, pageSize);
		supplierPromotionConfigRecordMapper.getSupplierPromotionConfigRecordList(productStoreId);
		return super.toPagedModels(pagedResult);
	}

	@Override
	public List<SupplierPromotionConfig> getSupplierPromotionConfigList(@RequestBody List<String> productStoreIds,
			@RequestParam(value = "configType", required = false) String configType) throws Exception {
		
		return promotionConfigService.getSupplierPromotionConfigList(productStoreIds, configType);
	}
	
	
	
	
}
